import { isUndefined } from '@web-utils/core'
import store from '@/store'
import { loadTheme } from '@/util'
import { minBy } from 'lodash-es'
import { getUrlFromPath } from '@/util/iframe'

const noTabPath = ['/403', '/404', '/500']

function findOldestTab(tabs) {
  let tab
  if (tabs.length === 1) {
    tab = tabs[0]
  } else {
    tab = minBy(tabs, value => (value.name === 'Home' ? Infinity : value.time))
  }
  return tab
}

function updateTabTitle(state, { path, newTitle }) {
  const index = state.tabs.findIndex(value => value.path === path)
  if (index > -1) {
    state.tabs[index].title = newTitle
  }
}

function updateTabFullPath(state, { path, newPath, time }) {
  const index = state.tabs.findIndex(value => value.path === path)
  if (index > -1) {
    state.tabs[index].fullPath = newPath
    state.tabs[index].time = time
  }
}

function updateTabTime(state, { path, title, newTime }) {
  const index = state.tabs.findIndex(value => value.path === path)
  if (index > -1) {
    state.tabs[index].time = newTime
    if (!isUndefined(title) && title !== null && state.tabs[index].title !== title) {
      state.tabs[index].title = title
    }
  }
}

export default {
  removeTabByPath(state, path) {
    const index = state.tabs.findIndex(value => value.path === path)
    if (index > -1) {
      state.tabs.splice(index, 1)
    }
  },
  addTabByRoute(state, route) {
    if (store.getters['user/isTempUser'] && route.path === '/') {
      return
    }

    if (noTabPath.includes(route.path)) {
      return
    }
    const time = Date.now()
    const rawMeta = route.meta
    let name = route.name
    let path = route.path
    let meta = route.meta
    const tabTitle = route.query.tabTitle
    const icon = route.query.icon
    if (!meta.title && route.matched[1]) {
      // 不带标题的路由使用父路由的标题
      name = route.matched[1].components.default.name
      path = route.matched[1].path
      meta = route.matched[1].meta
    }
    const isExist = state.tabs.some(item => item.path === path)
    if (!isExist) {
      // 不存在才能添加到页签列表
      if (state.tabs.length >= 100) {
        // 最多允许放置30个标签
        const tab = findOldestTab(state.tabs)
        state.tabs = state.tabs.filter(value => value !== tab)
      }
      state.tabs.push({
        fullPath: route.fullPath,
        title: tabTitle || meta.title,
        icon: icon || meta.icon,
        url: getUrlFromPath(route),
        path,
        name,
        time
      })
    } else {
      if (!rawMeta.title) {
        // 路由没配置标题不开标签
        updateTabFullPath(state, {
          path: route.matched[1].path,
          title: tabTitle || meta.title,
          newPath: route.fullPath,
          time
        })
      } else {
        updateTabTime(state, { path: route.path, title: tabTitle || meta.title, newTime: Date.now() })
      }
    }
  },
  setTabs(state, tabs) {
    state.tabs = tabs
  },
  updateTabTitle,
  updateTabTime,
  updateTabFullPath,
  clearViewCache(state) {
    state.tabs.splice(1, state.tabs.length)
  },
  toggleSidebar(state) {
    state.collapse = !state.collapse
  },
  adjustSidebar(state) {
    state.collapse = false
    // state.collapse = document.body.clientWidth < 1500
  },
  async setTheme(state, theme) {
    state.theme.name = theme.name
    state.theme.value = theme.value
    loadTheme(theme)
  },
  setThemeColor(state, themeColor) {
    state.themeColor = themeColor
  }
}
